import load from '@/utils/jsapi.js';
import JSON from 'json3';
import mapconfig from '@/config/config_map.js'

const findNearpointsOperate = {
  async findNearpointsbydistance(Parameters) {
    const [LabelClass,
      array,
      Graphic,
      Point,
      geometryEngine,
      Polyline,
      Polygon,
      SpatialReference,
      FeatureLayer,
      GraphicsLayer,
      Legend
    ] = await load(["esri/layers/support/LabelClass",
      "dojo/_base/array",
      "esri/Graphic",
      "esri/geometry/Point",
      "esri/geometry/geometryEngine",
      "esri/geometry/Polyline",
      "esri/geometry/Polygon",
      "esri/geometry/SpatialReference",
      "esri/layers/FeatureLayer",
      "esri/layers/GraphicsLayer",
      "esri/widgets/Legend"
    ]);

    let codX = 0;
    if (Parameters.codX != undefined && Parameters.codX != null) {
      codX = parseFloat(Parameters.codX)
    } else {
      return;
    }

    let codY = 0;
    if (Parameters.codY != undefined && Parameters.codY != null) {
      codY = parseFloat(Parameters.codY)
    } else {
      return;
    }

    let distance = 0;
    if (Parameters.distance != undefined && Parameters.distance != null) {
      distance = parseFloat(Parameters.distance)
    } else {
      return;
    }

    let layername;
    if (Parameters.layername != undefined && Parameters.layername != null) {
      layername = Parameters.layername;
    } else {
      return;
    }

    const findpointsinlayer = window.map.findLayerById(layername);
    if (findpointsinlayer == undefined || findpointsinlayer == null)
      return;

    let point = new Point({
      x: codX,
      y: codY,
      spatialReference: SpatialReference.WebMercator
    });

    const pointbuffer = geometryEngine.geodesicBuffer(point, distance, 'meters');
    let pointbufferGraphic = new Graphic({
      geometry: pointbuffer,
      symbol: mapconfig.PgHightlightsymbol
    });

    let findnearbuffer_Graphiclayer = window.map.findLayerById("findnearbuffer_Graphiclayer");
    if (Parameters.buffervisible == undefined || Parameters.buffervisible == true) {
      if (findnearbuffer_Graphiclayer == null || findnearbuffer_Graphiclayer == undefined) {
        findnearbuffer_Graphiclayer = new GraphicsLayer({
          id: "findnearbuffer_Graphiclayer",
          title: "findnearbuffer_Graphiclayer",
          elevationInfo: {
            mode: "absolute-height",
            offset: 1,
            unit: "meters",
          },
          listMode: "hide"
        })
        window.map.add(findnearbuffer_Graphiclayer);
      }
      findnearbuffer_Graphiclayer.removeAll();
      findnearbuffer_Graphiclayer.add(pointbufferGraphic);
    } else {
      if (findnearbuffer_Graphiclayer != null || findnearbuffer_Graphiclayer != undefined)
        findnearbuffer_Graphiclayer.removeAll();
    }

    const graphicsource = findpointsinlayer.source;
    let attrsarry=[];
    const nearpoints = graphicsource.map(function (item, index, ary) {
      const isinterest = geometryEngine.intersects(pointbuffer, new Point({
        x: item.geometry.x,
        y: item.geometry.y,
        spatialReference: SpatialReference.WebMercator
      }))
      if (isinterest === true) {
        let paths = [];
        paths.push([codX, codY]);
        paths.push([item.geometry.x, item.geometry.y]);
        const line = new Polyline({
          hasZ: false,
          hasM: true,
          paths: paths,
          spatialReference: {
            wkid: 102100
          }
        })
        const dist = geometryEngine.geodesicLength(line, 'meters');
        let attrs = item.attributes;
        attrs.distance = dist;
        attrsarry.push(attrs);
      }
    })
    if (attrsarry != undefined) {
      window.parent.postMessage(JSON.stringify({
        "action": "findnearpointsbydistance",
        "layername": Parameters.layername,
        "data": attrsarry
      }), '*')
    }
  }
};
export default findNearpointsOperate
